package com.jiyang.jyandroid.test01;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.jiyang.jyandroid.BaseActivity;
import com.jiyang.jyandroid.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jy on 2016/10/24.
 * 使用ArrayAdapter创建ListView
 */

public class One_8Activity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test01_4);
        ListView listView = (ListView) findViewById(R.id.list);
        //构建数据
        String[] names = new String[]{"小明", "小红", "小张"};
        String[] des = new String[]{
                "小明是陪伴我从小到大的好朋友",
                "每次写作文，我都能碰见小明和小红",
                "小张……其实和他不是很熟"};
        int[] imgIds = new int[]{R.mipmap.child01, R.mipmap.child02, R.mipmap.child03};
        //构建map集合
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", names[i]);
            map.put("des", des[i]);
            map.put("image", imgIds[i]);
            mapList.add(map);
        }
        //创建SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,
                mapList,
                R.layout.item_test01_5,
                new String[]{"name", "des", "image"},
                new int[]{R.id.tv_name, R.id.tv_des, R.id.iv});
        //设置到ListView
        listView.setAdapter(simpleAdapter);
    }
}
